<?php
// @formatter:off
/**
 * @file c.create.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-05-27 04:56:35 +0000 (Mon, 27 May 2013) $
 *
 * $Id: c.create.php 67 2013-05-27 04:56:35Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0.0.1/extensions/TagsRelator/controlers/c.create.php $
 */
// @formatter:on

global $wcSession;
global $wcRule;
global $wcRuleAction;

if($wcRule && $wcProfile->allowedTo(WC_PERM_RULE_EDIT)) {
	$created = false;

	if($_SERVER["REQUEST_METHOD"] == "POST") {
		global $wcRulesHolder;
		global $wcDatabaseAccess;

		$wcRule->indexed(false);

		$db = WCConnection::Instance();
		$dbprefix = $wcDatabaseAccess['prefix'];

		$stmt = null;
		$stmtId = __CLASS__."::".__FUNCTION__."[1]";
		if($db->has($stmtId)) {
			$stmt = $db->get($stmtId);
		} else {
			$query = "insert\n";
			$query .= "        into {$dbprefix}rule_actions (\n";
			$query .= "                rel_ra_rule, rel_ra_type, rel_ra_name, rel_ra_info)\n";
			$query .= "        values (:rule, :type, :name, :info)\n";

			$stmt = $db->prepare($stmtId, $query);
		}

		$params = array(
			":rule" => $wcRule->id(),
			":type" => TagsRelator::$TYPE,
			":name" => $_POST["ACTION_NAME"],
			":info" => serialize(array("mode" => $_POST["ACTION_MODE"]))
		);

		if($stmt->execute($params)) {
			$created = true;
			$id = $db->lastInsertId();
			$wcRule->count++;

			$wcRule->reload();
			foreach($wcRule->actions() as $action) {
				if($action->id() == $id) {
					$wcRuleAction = $action;
					break;
				}
			}
		}
	}

	$wcThemeAssigns["PATH"] = "Action Create: ".($created ? "'{$wcRuleAction->name()}' of " : "For ")."rule '{$wcRule->name()}'";
	if(!$created) {
		$wcThemeAssigns["form-action"] = requestParams(true, $wcAction, array(), false);
	} else {
		global $wcRuleHooks;

		$wcThemeAssigns["action-created"] = true;
		$wcThemeAssigns["name"] = $wcRuleAction->name();
		$wcThemeAssigns["mode"] = $wcRuleAction->mode;
		$wcThemeAssigns["visit"] = "?action=".WC_ACTION_CANVAS."&saction={$wcRuleHooks[TagsRelator::$TYPE]["edit"]}&group={$wcGroup->id()}&rule={$wcRule->id()}&raction={$wcRuleAction->id()}";
	}
} else {
	if(!$wcProfile->allowedTo(WC_PERM_RULE_EDIT)) {
		$wcThemeAssigns["error-message"] = "You don't have enough priviliges";
		require "{$this->_themeRoot}/controlers/contents/c.403.php";
	} else {
		$wcThemeAssigns["error-message"] = "There seems to be a problem with parameters";
		require "{$this->_themeRoot}/controlers/contents/c.500.php";
	}
}
?>